//
//  SceneDelegate.m
//  inquiry
//
//  Created by 高宇 on 2020/5/17.
//  Copyright © 2020 高宇. All rights reserved.
//

#import "SceneDelegate.h"
#import "LoginViewController.h"
#import "MyViewController.h"
#import "Interface.h"
#import "ULNetHelper.h"
#import "GXToast.h"
#import "NSString+MD5.h"
#import "TeacherViewController.h"
#import "HomeViewController.h"


@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    UIWindowScene *windowScene = (UIWindowScene *)scene;
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *type = [user objectForKey:@"type"];  //是否已有登录角色
    if ([type intValue] == 1001) {
        //自动登录
        //修改主视图逻辑
        HomeViewController *vc = [[HomeViewController alloc] init];
        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
        nc.tabBarItem.selectedImage = [[UIImage imageNamed:@"矩形(4)-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nc.tabBarItem.image = [[UIImage imageNamed:@"矩形(1)-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        nc.tabBarItem.title = @"测试";

        MyViewController *myvc = [[MyViewController alloc] init];
        UINavigationController *mync = [[UINavigationController alloc] initWithRootViewController:myvc];
        mync.tabBarItem.selectedImage = [[UIImage imageNamed:@"矩形(2)"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        mync.tabBarItem.image = [[UIImage imageNamed:@"矩形(5)-1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        mync.tabBarItem.title = @"我的";

        UITabBarController *tab=[[UITabBarController alloc] init];
        tab.viewControllers=@[nc,mync];
        self.window.rootViewController = tab;
    }
    else if ([type intValue] == 1002) {
        [user setObject:[self getNowTimeTimestamp] forKey:@"teacherLoginTime"];
        //老师页面
        TeacherViewController *vc = [[TeacherViewController alloc] init];
        UINavigationController *tab = [[UINavigationController alloc] initWithRootViewController:vc];
        self.window.rootViewController = tab;
    }else{
        
        self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
        self.window.frame = windowScene.coordinateSpace.bounds;
        [self.window makeKeyAndVisible];
        
        LoginViewController *vc = [[LoginViewController alloc] init];
        self.window.rootViewController = vc;
    }
    
}


- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


- (NSString *)nowTimeSwitchTimestamp{
    NSDate *datenow = [NSDate date];//现在时间
    //这里如果long不够用,就用(long long)
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
    return timeSp;
}

//返回值格式:2019-04-19 10:33:35.886
- (NSString *)getNowTimeTimestamp{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSString *dateString      = [formatter stringFromDate: datenow];
    NSLog(@"当前时间戳对应的时间是:%@",dateString);
    return dateString;
}

@end
